function showCharacterSelection() {
    // 隐藏主菜单
    document.getElementById('mainMenu').style.display = 'none';
    
    // 显示角色选择界面
    const characterSelect = document.getElementById('characterSelect');
    characterSelect.style.display = 'block';
    
    // 清空现有内容
    const avatarsContainer = characterSelect.querySelector('.character-avatars');
    avatarsContainer.innerHTML = '';
    
    // 添加角色头像
    InitialCharacters.forEach(charId => {
        const character = characterManager.createCharacter(charId);
        
        const avatar = document.createElement('img');
        avatar.className = 'character-avatar';
        avatar.src = character.images.card;
        avatar.alt = character.name;
        
        avatar.addEventListener('click', () => {
            // 移除其他头像的选中状态
            document.querySelectorAll('.character-avatar').forEach(av => {
                av.classList.remove('selected');
            });
            
            // 添加选中状态
            avatar.classList.add('selected');
            
            // 更新角色详情
            updateCharacterDetail(character);
        });
        
        avatarsContainer.appendChild(avatar);
    });
    
    // 默认选中第一个角色
    const firstAvatar = avatarsContainer.firstChild;
    if (firstAvatar) {
        firstAvatar.click();
    }
}

function updateCharacterDetail(character) {
    const detail = document.querySelector('.character-detail');
    
    // 更新全身立绘
    detail.querySelector('.character-full').src = character.images.full;
    
    // 更新角色信息
    detail.querySelector('.character-name').textContent = character.name;
    detail.querySelector('.trait-name').textContent = character.trait.name;
    detail.querySelector('.trait-desc').textContent = character.trait.description;
    
    // 更新属性值
    detail.querySelector('.stat-value.hp').textContent = character.maxHp;
    detail.querySelector('.stat-value.attack').textContent = character.attack;
    detail.querySelector('.stat-value.defense').textContent = character.defense;
    detail.querySelector('.stat-value.speed').textContent = character.speed;
    
    // 更新选择按钮事件
    detail.querySelector('.select-btn').onclick = () => selectCharacter(character);
}

function startNewGame(character) {
    // 隐藏角色选择界面
    document.getElementById('characterSelect').style.display = 'none';
    
    // 显示关卡选择界面
    const levelSelect = document.getElementById('levelSelect');
    levelSelect.style.display = 'block';
    
    // 清空现有关卡列表
    const levelList = levelSelect.querySelector('.level-list');
    levelList.innerHTML = '';
    
    // 添加初始关卡
    const levelItem = document.createElement('li');
    levelItem.className = 'level-item';
    levelItem.innerHTML = `
        <div class="level-header">
            <span>关卡 1-1</span>
            <span>难度: ★</span>
        </div>
        <div class="level-content" style="display: none;">
            <div class="level-rewards">
                <h3>关卡奖励：</h3>
                <p>- 金币: 100</p>
                <p>- 随机装备 x1</p>
                <p>- 随机符卡 x1</p>
            </div>
            <button class="start-level-btn">开始挑战</button>
        </div>
    `;
    
    // 添加折叠功能
    const header = levelItem.querySelector('.level-header');
    const content = levelItem.querySelector('.level-content');
    header.addEventListener('click', () => {
        content.style.display = content.style.display === 'none' ? 'block' : 'none';
    });
    
    // 添加开始按钮功能
    const startBtn = levelItem.querySelector('.start-level-btn');
    startBtn.addEventListener('click', () => {
        startLevel(character, 1, 1);
    });
    
    levelList.appendChild(levelItem);
    
    // 添加返回按钮功能
    levelSelect.querySelector('.back-btn').addEventListener('click', () => {
        levelSelect.style.display = 'none';
        document.getElementById('characterSelect').style.display = 'block';
    });
}

function startLevel(character, world, level, saveData = null) {
    // 隐藏关卡选择界面
    document.getElementById('levelSelect').style.display = 'none';
    
    // 显示游戏界面
    const gameContainer = document.querySelector('.game-container');
    gameContainer.style.display = 'block';
    
    // 创建地图
    let levelMap;
    if (saveData?.currentNode) {
        levelMap = saveData.currentNode;
    } else {
        const mapGenerator = new LevelMapGenerator(LevelDifficulty.EASY);
        levelMap = mapGenerator.generateMap();
    }
    
    // 创建地图视图
    const mapView = new LevelMapView(gameContainer, character);
    
    // 如果有存档的地图状态，应用它
    if (saveData?.mapState) {
        mapView.scale = saveData.mapState.scale;
        mapView.offsetX = saveData.mapState.offsetX;
        mapView.offsetY = saveData.mapState.offsetY;
    }
    
    mapView.createMap(levelMap);
    
    // 保存游戏状态
    saveGame({
        character,
        world,
        level,
        currentNode: levelMap,
        mapView
    });
}

function saveGame(gameState) {
    localStorage.setItem('gameState', JSON.stringify(gameState));
}

// 修改角色选择函数
function selectCharacter(character) {
    ModalManager.showMessage({
        title: '角色选择',
        message: `选择了角色：${character.name}`,
        confirmText: '开始游戏'
    }).then(() => {
        startNewGame(character);
    });
}

// 添加返回按钮功能
document.addEventListener('DOMContentLoaded', () => {
    const buttons = document.querySelectorAll('.menu-btn');
    buttons.forEach(button => {
        button.addEventListener('click', () => {
            if (button.textContent === '新游戏') {
                showCharacterSelection();
            } else if (button.textContent === '读取存档') {
                showLoadGameDialog();
            } else if (button.textContent === '设置') {
                window.open('help.html', '_blank');
            }
        });
    });
    
    // 添加返回按钮事件
    document.querySelector('.back-btn').addEventListener('click', () => {
        document.getElementById('characterSelect').style.display = 'none';
        document.getElementById('mainMenu').style.display = 'block';
    });
});

function showLoadGameDialog() {
    const saveData = SaveSystem.load();
    if (!saveData) {
        ModalManager.showMessage({
            title: '提示',
            message: '没有找到存档！'
        });
        return;
    }

    ModalManager.show('#save-modal', {
        onShow: (content) => {
            content.querySelector('.character-preview img').src = saveData.character.images.full;
            content.querySelector('.character-name').textContent = 
                `${saveData.character.name} Lv.${saveData.character.level}`;
            content.querySelector('.save-details').innerHTML = `
                <p>世界: ${saveData.world}-${saveData.level}</p>
                <p>金币: ${saveData.character.gold}</p>
                <p>存档时间: ${SaveSystem.formatDate(saveData.timestamp)}</p>
            `;

            content.querySelector('.load-btn').onclick = () => {
                ModalManager.hide('.modal');
                document.getElementById('mainMenu').style.display = 'none';
                startLevel(saveData.character, saveData.world, saveData.level, saveData);
            };

            content.querySelector('.cancel-btn').onclick = () => {
                ModalManager.hide('.modal');
            };
        }
    });
} 
function showNotification(title, message, type) {
    
}
